﻿using System;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Input;
using SnakeGame.Models;
using SnakeGame.Views;
using System.Windows.Media;

namespace SnakeGame.ViewModels
{
    class QuestionPageViewModel : NotifyPropertyChanged
    {
        MediaPlayer mplayer = new MediaPlayer();

        public ICommand StartCommand { get; set; }
        private QuestionFrame _qFrame;
        private DispatcherTimer _timer;
        private int _timeout;
        public int Timeout
        {
            get { return _timeout; }
            set
            {
                _timeout = value;
                OnPropertyChanged("Timeout");
            }
        }

        private Visibility _isTimeoutEnd;
        public Visibility TimeoutEnd
        {
            get { return _isTimeoutEnd; }
            set
            {
                _isTimeoutEnd = value;
                OnPropertyChanged("TimeoutEnd");
            }
        }

        private Visibility _showCountdown;
        public Visibility ShowCountdown
        {
            get { return _showCountdown; }
            set
            {
                _showCountdown = value;
                OnPropertyChanged("ShowCountdown");
            }
        }

        private Visibility _showStartButton;
        public Visibility ShowStartButton
        {
            get { return _showStartButton; }
            set
            {
                _showStartButton = value;
                OnPropertyChanged("ShowStartButton");
            }
        }

        public string Quest { get; set; }

        public event EventHandler RequestClose;

        protected void OnRequestClose()
        {
            if (_timer != null)
            {
                _timer.Stop();
            }
            if (RequestClose != null)
            {
                RequestClose(this, EventArgs.Empty);
            }
        }

        public QuestionPageViewModel()
        {
            StartCommand = new RelayCommand(OnStartCommand);
        }

        private void OnStartCommand(object obj)
        {
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Start();
            ShowCountdown = Visibility.Visible;
            ShowStartButton = Visibility.Hidden;

            // play sound
            _qFrame.MediaPlayer.Open(new Uri("ticktock.wav", UriKind.Relative));
            _qFrame.MediaPlayer.Play();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Timeout -= 1;
            if (Timeout <= 0)
            {
                _timer.Stop();
                _qFrame.MediaPlayer.Stop();
                Timeout = 0;
                TimeoutEnd = Visibility.Visible;
                ShowCountdown = Visibility.Hidden;

                JudgePage jPage = new JudgePage();
                jPage.Initialize(_qFrame);
                _qFrame.Navigate(jPage);

            }
        }


        public void Initialize(QuestionFrame qf)
        {
            _qFrame = qf;
            Quest = qf.SelectedQuestion.Quest;
            Timeout = qf.QuestionItem.Timeout;

            
            TimeoutEnd = Visibility.Hidden;
            ShowCountdown = Visibility.Hidden;
            ShowStartButton = Visibility.Visible;
        }

    }
}
